﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RecognitionForm.Core;

namespace RecognitionForm.Handler
{

    public class StaffInfoDal : BaseDal<StaffInfo>, IStaffInfoDal
    {
        public async Task<IEnumerable<StaffInfo>> GetStaffInfosByDepart(string depart)
        {
            string sql = @"Select * From [StaffInfo] where depart like @depart order by  birthday desc";
            return await QueryAsync(sql, new { depart = depart + "%" });
        }
    }



    public class RecognitionLogDal : BaseDal<RecognitionLogInfo>, IRecognitionLogDal
    {
        public async Task<int> GetScalarLog(string staffcode, DateTime startTime, DateTime endTime)
        {
            string sql = "Select 1 From [RecognitionLog] where [staffcode]=@staffcode and [credate] between @begintime and @endtime limit 1";
            var result = await QueryAsyncDynamic(sql, new { staffcode = staffcode, begintime = startTime, endtime = endTime });
            if (result != null && result.Count() > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }


        public async Task<IEnumerable<dynamic>> GetLastLogs(DateTime startTime, DateTime endTime)
        {
            string sql = @"select b.name,max([credate]) as lastTime from [RecognitionLog] a 
                        inner join [StaffInfo] b on a.[staffcode] = b.[code]
                        where [credate]>@startTime and [credate]<@endTime
                        group by staffcode
                        order by lastTime";
            return await QueryAsyncDynamic(sql, new { startTime = startTime, endTime = endTime });
        }
    }

}
